package Repository;

import java.util.HashMap;
import util.SMSUtil;
import model.Account;
import model.Rule;

import java.util.Iterator;


public class AccountRepository {
	
	public static HashMap<Long,Account> accountHash = new HashMap<Long,Account>();
	
	/*static
	{
	    Account account = new Account();
		account.setAccountName("Tioga");
		account.setAccountId(1);
		accountHash.put(account.getAccountId(), account);
		
		
		account = new Account();
		account.setAccountName("Monet");
		account.setAccountId(2);
		accountHash.put(account.getAccountId(), account);
		
		account = new Account();
		account.setAccountName("Kroger");
		account.setAccountId(3);
		accountHash.put(account.getAccountId(), account);
		
		
		account = new Account();
		account.setAccountName("Kiwi");
		account.setAccountId(4);
		accountHash.put(account.getAccountId(), account);
		
		
		account = new Account();
		account.setAccountName("Sam");
		account.setAccountId(5);
	    accountHash.put(account.getAccountId(), account);
		
	}*/
	
	
	
	public void delete() 
	{
		retrieve();
		System.out.println("Enter account id to delete: ");
		String accountid = SMSUtil.getUserInput();
		accountHash.remove(Long.parseLong(accountid.trim()));
		System.out.println("Successfully deleted account");
		retrieve();
		
	}


	public Account insert() 
	{
		System.out.println("Enter account details :- ");
		System.out.println("Enter new account name: ");
		String accountname = SMSUtil.getUserInput();
		Account account = new Account();
		account.setAccountName(accountname);
		account.setAccountId(accountHash.size() + 1);
		accountHash.put(account.getAccountId(), account);
		System.out.println("Successfully created account!!");
		return account;
		
	}

	public void retrieve() 
	{
	    Iterator iterator = accountHash.keySet().iterator();
	    SMSUtil.displayAccountHeader();
		while (iterator.hasNext())
		{
			Long id = (Long) iterator.next();
			Account account = accountHash.get(id.longValue());
			SMSUtil.formatAccount(account);
		}
			
	}

	
	public void update() 
	{
		
		
		retrieve();
		System.out.println("Enter id of the old account:");
		String accountid = SMSUtil.getUserInput();
		System.out.println("Enter new account name to update:");
		String accountname = SMSUtil.getUserInput();
		
		Account account = new Account();
		account.setAccountName(accountname);
		account.setAccountId(Long.parseLong(accountid.trim()));
    	accountHash.put(Long.parseLong(accountid.trim()), account);
     	System.out.println("Successfully updated account.");
     	retrieve();

		
	}
	
		

}
